package com.tydic.portal.controller;

import com.tydic.common.core.domain.R;
import com.tydic.system.domain.vo.SysOssVo;
import com.tydic.system.service.ISysOssService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Validated
@RestController
@RequestMapping(value = "/file")
@Slf4j
@RequiredArgsConstructor
public class FileController {
    private final ISysOssService sysOssService;


    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile multipartFile) {
        SysOssVo upload = sysOssService.upload(multipartFile);
        if (upload == null) {
            return R.fail("上传文件失败");
        } else {
            return R.ok("上传文件成功", upload.getOssId());
        }
    }

    /**
     * 预览文件
     *
     * @param fileId
     * @param response
     */
    @GetMapping("/preview")
    public void preview(@NotBlank(message = "文件ID不能为空") @RequestParam("fileId") String fileId, HttpServletResponse response) {
        sysOssService.preview(fileId, response);
    }

    /**
     * 下载文件
     *
     * @param fileId
     * @param response
     */
    @GetMapping("/download")
    public void download(@NotBlank(message = "文件ID不能为空") @RequestParam("fileId") String fileId, HttpServletResponse response) {
        sysOssService.download(fileId, response);
    }
}
